package com.mrli.cat.weather.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

import com.mrli.cat.weather.R;
import com.mrli.cat.weather.database.WeatherContract;
import com.mrli.cat.weather.model.City;
import com.mrli.cat.weather.service.WeatherUpdateService;
import com.mrli.cat.weather.tools.WriteCityAndUpdate;
import com.mrli.cat.weather.view.WeatherAppWidgetProvider;

public class WeatherPreferenceFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
    private final static int SELECT_CITY = 0x100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onStart() {
        super.onStart();
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
        PreferenceScreen screen = getPreferenceScreen();
        screen.findPreference(WeatherPreferenceActivity.LOCAL_CITY).setSummary(sharedPreferences.getString(WeatherPreferenceActivity.LOCAL_CITY, ""));
        screen.findPreference(WeatherPreferenceActivity.LOCAL_CITY).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                //手动选择城市
                Intent selectcityInten = new Intent(getActivity(), SelectCityActivity.class);
                startActivityForResult(selectcityInten, SELECT_CITY);
                return true;
            }
        });
        int days = sharedPreferences.getInt(WeatherPreferenceActivity.FORECAST_DAY, 7);
        screen.findPreference(WeatherPreferenceActivity.FORECAST_DAY).setSummary(days + "天");
        int value = Integer.valueOf(sharedPreferences.getString(WeatherPreferenceActivity.METRIC_KEY, ""));
        screen.findPreference(WeatherPreferenceActivity.METRIC_KEY).setSummary(value == 0 ? "摄氏度°C" : "华氏度°F");
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == SELECT_CITY) {
            if (resultCode == SelectCityFragment.RESULT_OK) {
                //提取用户选择的城市信息， 并写入文件和数据库, 然后请求同步
                City city = data.getExtras().getParcelable(SelectCityFragment.CITY);
                new WriteCityAndUpdate(getActivity()).execute(city);
                //通知前台数据改变
                getActivity().getContentResolver().notifyChange(WeatherContract.WEATHER_CONTENT_URI, null);
                getActivity().startService(new Intent(getActivity(), WeatherUpdateService.class));
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals(WeatherPreferenceActivity.METRIC_KEY)) {
            Preference connectionPref = findPreference(key);
            int value = Integer.valueOf(sharedPreferences.getString(WeatherPreferenceActivity.METRIC_KEY, ""));
            connectionPref.setSummary(value == 0 ? "摄氏度°C" : "华氏度°F");
        }
        if (key.equals(WeatherPreferenceActivity.FORECAST_DAY)) {
            Preference connectionPref = findPreference(key);
            connectionPref.setSummary(sharedPreferences.getInt(WeatherPreferenceActivity.FORECAST_DAY, 7) + "天");
        }
        if(key.equals(WeatherPreferenceActivity.LOCAL_CITY)){
            Preference connectionPref = findPreference(key);
            connectionPref.setSummary(sharedPreferences.getString(WeatherPreferenceActivity.LOCAL_CITY,""));
        }
        getActivity().getContentResolver().notifyChange(WeatherContract.WEATHER_CONTENT_URI, null);
        getActivity().startService(new Intent(getActivity(), WeatherUpdateService.class));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }
}
